var cusUnique;//用户唯一性标识
var cusFlag;//客户身份 0-注册用户  1-游客
var dest;//
var addrId=0;//

$(document).ready(function() {
	var thisURL = document.URL;  
	var  getval =thisURL.split('?')[1].split('&'); 
	cusUnique=getval[0].split("=")[1];
	dest=getval[1].split("=")[1];
	if(dest==1){
		$("#addressTitle").text('编辑地址');
		addrId=getCookie("addrId");
		loadAddress(addrId);
	}else{
		$("#addressTitle").text(添加新地址);
	}

});

// 加载地址
function loadAddress(addrId) {
	$.ajax({
		url : "../cus/findAddrByIdA.do",
		type : "post",
		data : {
			"addrId" : addrId
		},
		dataType : "json",
		success : function(result) {
			console.log(result);
			if (result.status == 1) {// 查询成功
				
				$("#pcusName").val(result.data.addrReceiverName);
				$("#pcusAddress").val(result.data.addrAddr);
				$("#pcusPhoneRE").val(result.data.addrPhone);
				addrId=result.data.addrId;
				
			} else {

			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
		}
	});
}

//添加编辑地址
function saveAddress(){
	var addrAddr=$("#pcusAddress").val();//收货人地址
	var addrPhone=$("#pcusPhoneRE").val();//收货人联系电话
	var addrReceiverName=$("#pcusName").val();//收货人姓名
	var ok=true;
	if(addrAddr==""){
		$("#pcusAddress").focus();
		ok=false;
		return;
	}
	if(addrPhone==""){
		$("#pcusPhoneRE").focus();
		ok=false;
		return;
	}
	if(addrReceiverName==""){
		$("#pcusName").focus();
		ok=false;
		return;
	}
	
	if(true){
		$.ajax({
			url : "../cus/updateAddrByIdP.do",
			type : "post",
			data : {"cusUnique":cusUnique,"addrId":addrId,"addrAddr":addrAddr,"addrPhone":addrPhone,"addrReceiverName":addrReceiverName},
			dataType : "json",
			success : function(result) {
				if (result.status == 1) {// 查询成功
					window.location.href="address.html?cusUnique="+cusUnique+"&dest="+3;
				} else {
					alert("请重新");
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {

			}
		});
	}
	
	
	
}


